﻿package 
{
    import DelayMessage.*;
    import com.assist.server.*;
    import com.assist.view.*;
    import com.assist.view.controls.*;
    import com.assist.view.interfaces.*;
    import com.lang.client_resources.delay_message.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.*;

    public class DelayMessage extends MovieClip implements IDelayMessage
    {
        public var _Container:MovieClip;
        public var _ClearCurrentMessage:MovieClip;
        private var _dialog:IAlert;
        private var _tip:ITip;
        private var _aryShowMessage:Array;
        private var _aryHideMessage:Array;
        private var _aryRemoveMessage:Array;
        private var _aryLimitId:Array;
        private var _objOverrideMessageID:Object;
        private var _intBtnSpace:int = 50;
        private var _timer:Timer;
        private var _whenDelayMessageDialogShow:Function;
        private var _specialDelayMessage:Function;
        private var _removeNickNameSuffix:Function;

        public function DelayMessage()
        {
            this._aryShowMessage = [];
            this._aryHideMessage = [];
            this._aryRemoveMessage = [];
            this._aryLimitId = [];
            this._objOverrideMessageID = {};
            this._whenDelayMessageDialogShow = new Function();
            this._specialDelayMessage = new Function();
            this._removeNickNameSuffix = new Function();
            return;
        }// end function

        public function get content() : MovieClip
        {
            return this;
        }// end function

        public function set tip(param1:ITip) : void
        {
            this._tip = param1;
            return;
        }// end function

        public function set dialog(param1:IAlert) : void
        {
            this._dialog = param1;
            return;
        }// end function

        override public function get width() : Number
        {
            return 500;
        }// end function

        public function init() : void
        {
            this._timer = new Timer(10);
            this._timer.addEventListener(TimerEvent.TIMER, this.onTimer);
            this._timer.stop();
            this.mouseEnabled = false;
            this._Container.mouseEnabled = false;
            this._ClearCurrentMessage.visible = false;
            this._ClearCurrentMessage.mouseEnabled = false;
            this._ClearCurrentMessage._Text.htmlText = HtmlText.format(DelayMessageLang.ClearCurrentMessage, 47086, 12, "", false, false, true);
            this._ClearCurrentMessage._Btn.addEventListener(MouseEvent.CLICK, this.onClearCurrentMessage);
            return;
        }// end function

        public function clear() : void
        {
            this._ClearCurrentMessage._Btn.removeEventListener(MouseEvent.CLICK, this.onClearCurrentMessage);
            this._timer.removeEventListener(TimerEvent.TIMER, this.onTimer);
            this._timer.stop();
            return;
        }// end function

        public function addMessage(param1:Object) : void
        {
            var _loc_3:Object = null;
            var _loc_2:* = this.getDelayMessageBtn(param1["id"]);
            _loc_2["id"] = param1["id"];
            _loc_2["flags"] = param1["flags"];
            _loc_2["clickCallBack"] = param1["clickCallBack"];
            _loc_2["message"] = this.setDelayMessageColor(param1["message"]);
            if (this.overrideOldMessage(this._aryShowMessage, _loc_2))
            {
                return;
            }
            if (this.overrideOldMessage(this._aryHideMessage, _loc_2))
            {
                return;
            }
            if (this._aryShowMessage.length < 10)
            {
                this.addShowMessage(_loc_2);
                this.setDelayMessagePoint();
            }
            else
            {
                this._aryHideMessage.push(_loc_2);
            }
            for each (_loc_3 in this._aryLimitId)
            {
                
                this.limitMessageAmount(_loc_3["list_id"], _loc_3["amount"]);
            }
            this._specialDelayMessage(_loc_2);
            _loc_2.addEventListener(MouseEvent.CLICK, this.onDelayMessageClick);
            this.addTip(_loc_2);
            return;
        }// end function

        public function addOverrideMessage(param1:int) : void
        {
            this._objOverrideMessageID[param1] = param1;
            return;
        }// end function

        public function addLimitMessage(param1:Array, param2:int) : void
        {
            var _loc_3:Object = {};
            _loc_3["list_id"] = param1;
            _loc_3["amount"] = param2;
            this._aryLimitId.push(_loc_3);
            return;
        }// end function

        public function reposition(param1:Number, param2:Number) : void
        {
            this.x = (param1 - this.width) / 2;
            if (this.x < 350)
            {
                this.x = 350;
            }
            this.y = param2 - 180;
            return;
        }// end function

        private function addShowMessage(param1:SimpleButton) : void
        {
            this._aryShowMessage.push(param1);
            param1.x = Math.floor((this.width - this._aryShowMessage.length * this._intBtnSpace) / 2) + (this._aryShowMessage.length - 1) * this._intBtnSpace;
            param1.y = 30;
            param1.alpha = 0;
            this._Container.addChild(param1);
            return;
        }// end function

        private function setDelayMessagePoint() : void
        {
            var _loc_2:SimpleButton = null;
            var _loc_1:* = Math.floor((this.width - this._aryShowMessage.length * this._intBtnSpace) / 2);
            for each (_loc_2 in this._aryShowMessage)
            {
                
                _loc_2["offsetX"] = _loc_1;
                _loc_2["offsetY"] = 0;
                _loc_1 = _loc_1 + this._intBtnSpace;
            }
            this._ClearCurrentMessage.visible = this._aryShowMessage.length >= 5;
            this._ClearCurrentMessage.mouseEnabled = this._aryShowMessage.length >= 5;
            if (!this._timer.running)
            {
                this._timer.start();
            }
            return;
        }// end function

        private function overrideOldMessage(param1:Array, param2:SimpleButton) : Boolean
        {
            var _loc_3:SimpleButton = null;
            for each (_loc_3 in param1)
            {
                
                if (_loc_3["id"] != DelayNotifyType.AddFriendIsAdd)
                {
                }
                if (_loc_3["id"] == DelayNotifyType.AddFriend)
                {
                }
                if (_loc_3["message"] == param2["message"])
                {
                    return true;
                }
                if (_loc_3["id"] == param2["id"])
                {
                    if (this._objOverrideMessageID.hasOwnProperty(param2["id"]))
                    {
                        _loc_3["message"] = param2["message"];
                        return true;
                    }
                }
            }
            return false;
        }// end function

        private function limitMessageAmount(param1:Array, param2:int) : void
        {
            var _loc_5:SimpleButton = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_6:Object = {};
            for each (_loc_7 in param1)
            {
                
                _loc_6[_loc_7] = _loc_7;
            }
            for each (_loc_5 in this._aryShowMessage)
            {
                
                if (_loc_6.hasOwnProperty(_loc_5["id"]))
                {
                    _loc_3 = _loc_3 + 1;
                }
            }
            for each (_loc_5 in this._aryHideMessage)
            {
                
                if (_loc_6.hasOwnProperty(_loc_5["id"]))
                {
                    _loc_4 = _loc_4 + 1;
                }
                if (_loc_3 + _loc_4 > param2)
                {
                    break;
                }
            }
            if (_loc_3 + _loc_4 > param2)
            {
                if (_loc_3 > 0)
                {
                    for each (_loc_5 in this._aryShowMessage)
                    {
                        
                        if (_loc_6.hasOwnProperty(_loc_5["id"]))
                        {
                            this.removeDelayMessage(_loc_5);
                            return;
                        }
                    }
                }
                else
                {
                    _loc_8 = 0;
                    while (_loc_8 < this._aryHideMessage.length)
                    {
                        
                        _loc_5 = this._aryHideMessage[_loc_8] as SimpleButton;
                        if (_loc_6.hasOwnProperty(_loc_5["id"]))
                        {
                            this._aryHideMessage.splice(_loc_8, 1);
                            return;
                        }
                        _loc_8 = _loc_8 + 1;
                    }
                }
            }
            return;
        }// end function

        private function addTip(param1:SimpleButton) : void
        {
            var _loc_2:* = new TextField();
            _loc_2.autoSize = TextFieldAutoSize.LEFT;
            _loc_2.textColor = 16777215;
            _loc_2.htmlText = param1["message"];
            if (_loc_2.width > 238)
            {
                _loc_2.multiline = true;
                _loc_2.wordWrap = true;
                _loc_2.width = 238;
                _loc_2.autoSize = TextFieldAutoSize.NONE;
                _loc_2.htmlText = param1["message"];
            }
            _loc_2.height = 22 + (_loc_2.numLines - 1) * 16;
            var _loc_3:* = new TextFormat("", 12);
            _loc_3.leading = 2;
            _loc_2.setTextFormat(_loc_3);
            this._tip.addTarget(param1, _loc_2);
            return;
        }// end function

        private function setDelayMessageColor(param1:String) : String
        {
            var _loc_5:String = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:String = null;
            var _loc_9:String = null;
            var _loc_10:String = null;
            var _loc_11:String = null;
            var _loc_2:* = param1.split("<");
            var _loc_3:String = "";
            var _loc_4:Object = {Y:HtmlText.Yellow, R:HtmlText.Red, W:HtmlText.White, G:HtmlText.Green, P:HtmlText.Purple, B:HtmlText.Blue2};
            for each (_loc_5 in _loc_2)
            {
                
                _loc_6 = 0;
                _loc_7 = _loc_5.lastIndexOf(">");
                if (_loc_7 != -1)
                {
                    _loc_8 = _loc_5.substr(_loc_6, _loc_7 - _loc_6 + 1);
                    _loc_9 = _loc_8.slice((_loc_8.lastIndexOf("&") + 1), (_loc_8.length - 1));
                    _loc_10 = _loc_8.slice(_loc_6, _loc_8.lastIndexOf("&"));
                    if (_loc_10.slice((_loc_10.length - 1)) == "】")
                    {
                        _loc_10 = _loc_10.substr(0, (_loc_10.length - 1));
                        _loc_10 = this._removeNickNameSuffix(_loc_10);
                        _loc_10 = _loc_10 + "】";
                    }
                    else
                    {
                        _loc_10 = this._removeNickNameSuffix(_loc_10);
                    }
                    _loc_11 = HtmlText.format(_loc_10, _loc_4[_loc_9], 12);
                    _loc_5 = _loc_5.replace(_loc_8, _loc_11);
                }
                _loc_3 = _loc_3 + _loc_5;
            }
            return _loc_3;
        }// end function

        private function getDelayMessageBtn(param1:int) : SimpleButton
        {
            var _loc_2:SimpleButton = null;
            switch(DelayNotifyType.getType(param1))
            {
                case DelayNotifyType.System:
                {
                    _loc_2 = new SystemMessageBtn();
                    break;
                }
                case DelayNotifyType.Email:
                {
                    _loc_2 = new PlayerMessageBtn();
                    break;
                }
                case DelayNotifyType.PK:
                {
                    _loc_2 = new PkMessageBtn();
                    break;
                }
                case DelayNotifyType.Flower:
                {
                    _loc_2 = new FlowerMessageBtn();
                    break;
                }
                default:
                {
                    _loc_2 = new SystemMessageBtn();
                    break;
                }
            }
            return _loc_2;
        }// end function

        private function removeDelayMessage(param1:SimpleButton) : void
        {
            var _loc_3:SimpleButton = null;
            param1.removeEventListener(MouseEvent.CLICK, this.onDelayMessageClick);
            this._tip.removeTarget(param1);
            var _loc_2:* = this._aryShowMessage.length - 1;
            while (_loc_2 >= 0)
            {
                
                if (param1 == this._aryShowMessage[_loc_2])
                {
                    this._aryShowMessage.splice(_loc_2, 1);
                    this._aryRemoveMessage.push(param1);
                    param1["offsetY"] = -30;
                    break;
                }
                _loc_2 = _loc_2 - 1;
            }
            if (this._aryShowMessage.length < 10)
            {
            }
            if (this._aryHideMessage.length > 0)
            {
                _loc_3 = this._aryHideMessage[0] as SimpleButton;
                this._aryHideMessage.splice(0, 1);
                this.addShowMessage(_loc_3);
            }
            this.setDelayMessagePoint();
            return;
        }// end function

        private function onDelayMessageClick(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget as SimpleButton;
            this._whenDelayMessageDialogShow(_loc_2["id"]);
            if (!_loc_2["yesLabel"])
            {
            }
            this._dialog.yesLabel = DelayMessageLang.DialogYes;
            if (!_loc_2["noLabel"])
            {
            }
            this._dialog.noLabel = DelayMessageLang.DialogNo;
            if (!_loc_2["flags"])
            {
            }
            if (!_loc_2["clickCallBack"])
            {
            }
            this._dialog.show(_loc_2["message"], AlertButtonType.Yes, null);
            this.removeDelayMessage(_loc_2);
            return;
        }// end function

        private function onClearCurrentMessage(event:MouseEvent) : void
        {
            var _loc_3:SimpleButton = null;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:SimpleButton = null;
            var _loc_2:* = this._aryShowMessage.length - 1;
            while (_loc_2 >= 0)
            {
                
                _loc_3 = this._aryShowMessage[_loc_2] as SimpleButton;
                _loc_3.removeEventListener(MouseEvent.CLICK, this.onDelayMessageClick);
                this._tip.removeTarget(_loc_3);
                this._aryShowMessage.splice(_loc_2, 1);
                this._aryRemoveMessage.push(_loc_3);
                _loc_3["offsetY"] = -30;
                _loc_2 = _loc_2 - 1;
            }
            if (this._aryHideMessage.length > 0)
            {
                _loc_4 = this._aryHideMessage.splice(0, this._aryHideMessage.length > 10 ? (10) : (this._aryHideMessage.length));
                _loc_5 = Math.floor((this.width - _loc_4.length * this._intBtnSpace) / 2);
                for each (_loc_6 in _loc_4)
                {
                    
                    _loc_6.x = _loc_5;
                    _loc_6.y = 30;
                    _loc_6["offsetY"] = 0;
                    _loc_6.alpha = 0;
                    this._aryShowMessage.push(_loc_6);
                    this._Container.addChild(_loc_6);
                    _loc_5 = _loc_5 + this._intBtnSpace;
                }
            }
            this.setDelayMessagePoint();
            return;
        }// end function

        private function onTimer(event:TimerEvent) : void
        {
            var _loc_3:SimpleButton = null;
            var _loc_4:Boolean = false;
            var _loc_5:int = 0;
            var _loc_6:SimpleButton = null;
            var _loc_2:Boolean = true;
            for each (_loc_3 in this._aryShowMessage)
            {
                
                if (_loc_3.x > _loc_3["offsetX"])
                {
                    _loc_3.x = _loc_3.x - Math.ceil((_loc_3.x - _loc_3["offsetX"]) / 10);
                }
                if (_loc_3.x < _loc_3["offsetX"])
                {
                    _loc_3.x = _loc_3.x + Math.ceil((_loc_3["offsetX"] - _loc_3.x) / 10);
                }
                if (_loc_3.y > _loc_3["offsetY"])
                {
                    var _loc_9:* = _loc_3;
                    var _loc_10:* = _loc_3.y - 1;
                    _loc_9.y = _loc_10;
                }
                if (_loc_3.y < _loc_3["offsetY"])
                {
                    var _loc_9:* = _loc_3;
                    var _loc_10:* = _loc_3.y + 1;
                    _loc_9.y = _loc_10;
                }
                if (_loc_3.alpha < 1)
                {
                    _loc_3.alpha = _loc_3.alpha + 0.05;
                }
                if (_loc_3.x == _loc_3["offsetX"])
                {
                }
                if (_loc_3.y == _loc_3["offsetY"])
                {
                }
                if (_loc_3.alpha < 1)
                {
                    _loc_2 = false;
                }
            }
            _loc_4 = true;
            _loc_5 = this._aryRemoveMessage.length - 1;
            while (_loc_5 >= 0)
            {
                
                _loc_6 = this._aryRemoveMessage[_loc_5] as SimpleButton;
                if (_loc_6.alpha > 0)
                {
                    _loc_6.alpha = _loc_6.alpha - 0.05;
                }
                if (_loc_6.y > _loc_6["offsetY"])
                {
                    var _loc_7:* = _loc_6;
                    var _loc_8:* = _loc_6.y - 1;
                    _loc_7.y = _loc_8;
                    _loc_4 = false;
                }
                else
                {
                    this._aryRemoveMessage.splice(_loc_5, 1);
                    this._Container.removeChild(_loc_6);
                }
                _loc_5 = _loc_5 - 1;
            }
            if (_loc_2)
            {
            }
            if (_loc_4)
            {
                this._timer.stop();
            }
            return;
        }// end function

        public function set whenDelayMessageDialogShow(param1:Function) : void
        {
            this._whenDelayMessageDialogShow = param1;
            return;
        }// end function

        public function set specialDelayMessage(param1:Function) : void
        {
            this._specialDelayMessage = param1;
            return;
        }// end function

        public function set removeNickNameSuffix(param1:Function) : void
        {
            this._removeNickNameSuffix = param1;
            return;
        }// end function

    }
}
